#include "tftp_server.h"

#define CHIP_UNIQUE_ID_REG	((const __IO uint8_t *)0x1FF80050)

const char* system_build_date = __DATE__;
const char* system_build_time = __TIME__;


bool _sysinfo_open(char* name, uint8_t mode)
{
    if (mode != TFTP_RRQ) return false;
    
    if (0 == strcmp(name, "sysinfo")) {
        return true;
    } else {
        return false;
    }
}
const static char* sysinfo_msg = "{\n"
    "vendor: \"WiiHey Shanghai\",\n"
    "chip_id: \"%02X%02X%02X%02X-%02X%02X%02X%02X-%02X%02X%02X%02X\",\n"
    "build_date: \"%s\",\n"
    "build_time: \"%s\",\n"
    "}\n";


uint16_t (_sysinfo_read)(uint8_t blk, uint8_t* buffer)
{
    if (blk >= 1) {
        return sprintf((char*)buffer, sysinfo_msg,
                       CHIP_UNIQUE_ID_REG[0],CHIP_UNIQUE_ID_REG[1],CHIP_UNIQUE_ID_REG[2],CHIP_UNIQUE_ID_REG[3],
                       CHIP_UNIQUE_ID_REG[4],CHIP_UNIQUE_ID_REG[5],CHIP_UNIQUE_ID_REG[6],CHIP_UNIQUE_ID_REG[7],
                       CHIP_UNIQUE_ID_REG[8],CHIP_UNIQUE_ID_REG[9],CHIP_UNIQUE_ID_REG[10],CHIP_UNIQUE_ID_REG[11],
                       system_build_date,
                       system_build_time
                       );
    } else {
        return 0;
    }
}

void (_sysinfo_write)(uint8_t blk, uint8_t* buffer, uint16_t len)
{
}

void (_sysinfo_close)(void)
{
}

const tftp_server_plugin_t tftp_server_plugin_sysinfo = {
    _sysinfo_open,
    _sysinfo_read,
    _sysinfo_write,
    _sysinfo_close,
};

